import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {AppConfig} from '../config';
import {Company} from '../entity/company.model';
import {Observable} from 'rxjs/Rx';
import {InterceptorService} from '../service/interceptor/interceptor-service';

@Injectable()
export class CompanyService {
  private readonly param = 'sysCompanys';

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(
    private http: InterceptorService) {
  }

  getComponys(): Promise<Company[]> {
    return this.http.get(this.url)
      .toPromise()
      .then(res => {
        if (res.code === '200') {
          return res.data as Company[];
        } else {
          return [] as Company[];
        }
      })
      .catch(this.handleError);
  }


  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

  /**
   * 批量删除
   *
   * @param {number[]} menuIds
   * @returns {Promise<boolean>}
   * @memberof MenuService
   */
  public batchDelete(companyIds: number[]): Promise<any> {
    const apiUrl = `${this.url}?items=${companyIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(result => {
        try {
          switch (result.code) {
            case '200':
              return result;
            case '8001':
              return result;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }

  /**
   * 创建
   *
   * @param {number[]} menuIds
   * @returns {Promise<boolean>}
   * @memberof CompanyService
   */
  public create(company: Company): Promise<Company> {
    // const url = `http://192.168.15.150:8585/webapi/v1/sysCompanys`;

    return this.http.post(this.url, JSON.stringify(company))
      .toPromise()
      .then(result => {
        try {
          switch (result.code) {
            case '200':
              return result.data as Company;
            case '-1':
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }

  /**
   * 更新
   *
   * @param {Menu} menu
   * @returns {Promise<Menu>}
   * @memberof MenuService
   */
  public update(company: Company): Promise<Company> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(company))
      .toPromise()
      .then(() => company)
      .catch(this.handleError);
  }

  /**
   * 在同一级别菜单下是否存在相同的菜单名称
   *
   * @param {string} menuName
   * @param {number} menuId
   * @returns {Observable<boolean>}
   * @memberof MenuService
   */
  public hasCompanyBycompanyName(companyName: string, parentId: number, companyId: number): Observable<boolean> {
    const url = `${this.url}/companyName/${companyName}/parentId/${parentId}/companyId/${companyId}`;
    return this.http.get(url).map(response => {
      let code = response.code;
      switch (code) {
        case '3001':
          return true;
        case '3002':
          return false;
      }
    }).catch(this.handleError);
  }

}


// WEBPACK FOOTER //
// ./src/app/service/company.service.ts
